package com.example.myapplication.database;

import android.annotation.SuppressLint;
import android.content.Context;

import com.example.myapplication.bean.User;
import com.example.myapplication.dao.UserDao;

import java.util.List;


/**
 * 用户数据库工具类
 */
public class UserDBUtils {
    // 数据库版本
    public static final int VERSION = 1;

    private static UserDBUtils sqliteDB;


    private UserDBUtils(Context context) {
    }

    /**
     * 获取UserDBUtils的单例对象
     * @param context 上下文对象
     * @return UserDBUtils的单例对象
     */
    public synchronized static UserDBUtils getInstance(Context context) {
        if (sqliteDB == null) {
            sqliteDB = new UserDBUtils(context);
        }
        return sqliteDB;
    }



    /**
     * 根据用户id查询用户
     * @param user_id 用户id
     * @return 查询到的用户对象，如果不存在则返回null
     */
    @SuppressLint("Range")
    public User select(String user_id){
        User user = null;
        UserDao userDao = GreenDaoManager.getmInstance().getDaoSession().getUserDao();
        List<User> userList = userDao.loadAll();
        if (userList ==null || userList.size() ==0){
            return null;
        }
        for (int i = 0; i < userList.size(); i++) {
            if (user_id.equals(userList.get(i).getUser_id())){
                return userList.get(i);
            }
        }
        return user;
    }




    /**
     * 根据用户id获取头像
     * @param user_id 用户id
     * @return 头像url
     */
    @SuppressLint("Range")
    public String getHeadUrlByUserId(String user_id){
        String user = "";
        UserDao userDao = GreenDaoManager.getmInstance().getDaoSession().getUserDao();
        List<User> userList = userDao.loadAll();
        if (userList ==null || userList.size() ==0){
            return null;
        }
        for (int i = 0; i < userList.size(); i++) {
            if (user_id.equals(userList.get(i).getUser_id())){
                return userList.get(i).getHead_url();
            }
        }
        return user;
    }

    /**
     * 根据用户id获取用户名
     * @param user_id 用户id
     * @return 用户名
     */
    @SuppressLint("Range")
    public String getNameByUserId(String user_id){
        String user = "";
        UserDao userDao = GreenDaoManager.getmInstance().getDaoSession().getUserDao();
        List<User> userList = userDao.loadAll();
        if (userList ==null || userList.size() ==0){
            return null;
        }
        for (int i = 0; i < userList.size(); i++) {
            if (user_id.equals(userList.get(i).getUser_id())){
                return userList.get(i).getName();
            }
        }
        return user;
    }
    /**
     * 根据用户id获取用户名
     * @param user_id 用户id
     * @return 用户名
     */
    public String getHeadUrlBy(String user_id){
        String user = "";
        UserDao userDao = GreenDaoManager.getmInstance().getDaoSession().getUserDao();
        List<User> userList = userDao.loadAll();
        if (userList ==null || userList.size() ==0){
            return null;
        }
        for (int i = 0; i < userList.size(); i++) {
            if (user_id.equals(userList.get(i).getUser_id())){
                return userList.get(i).getHead_url();
            }
        }
        return user;
    }
}
